/*
    jQuery for easily manipulating text or image based buttons.
    Copyright (c) 2010 Keiji Hokamura (famteam)
    This plugin available for use in all personal or commercial projects under both MIT and GPL licenses.
 */

(function($) {
	
	$.fn.buttoner = function(settings) {
		settings = $.extend({
			toggle: false,
			overPostfix: '-over',
			group: false,
			type: $.buttoner.IMAGE,
			color: {
				normal: null,
				over: null
			},
			handlers: null,
			handler: null
		}, settings);

		var self = this;
		if (settings.type == $.buttoner.TEXT && settings.color.over != null && settings.color.normal == null) {
			settings.color.normal = self.css('color');
		}
		
		return self.each(function() {
			var link = $(this);
			if (settings.type == $.buttoner.IMAGE) {
				var linkImage = link.children('img');
				var imageSource = linkImage.attr('src');
				var postfixIndex = imageSource.lastIndexOf('.');
				var data = {
					normal: imageSource,
					over: imageSource.substring(0, postfixIndex) + '-over' + imageSource.substring(postfixIndex),
					clicked: false
				};
				link.data($.buttoner.DATA, data);
				link.mouseenter(function() {
					linkImage.attr('src', data.over);
				}).mouseleave(function() {
					if (!data.clicked) {
						linkImage.attr('src', data.normal);
					}
				}).click(function(event) {
					if (settings.toggle) {
						data.clicked = true;
						linkImage.attr('src', data.over);
						if (settings.group) {
							self.not(link).each(function(index) {
								var otherLink = $(this);
								var otherLinkData = otherLink.data($.buttoner.DATA);
								otherLink.children('img').attr('src', otherLinkData.normal);
								otherLinkData.clicked = false;
							});
						}
					}
					
					$.buttoner._handler(link, settings, event);
				});
			}
			else if (settings.color.over != null && settings.toggle) {
				var data = {
					clicked: false
				};
				link.data($.buttoner.DATA, data);
				link.mouseenter(function() {
					link.css('color', settings.color.over);
				}).mouseleave(function() {
					if (!data.clicked) {
						link.css('color', settings.color.normal);
					}
				}).click(function (event) {
					data.clicked = true;
					link.css('color', settings.color.over);
					if (settings.group) {
						self.not(link).css('color', settings.color.normal).each(function() {
							var otherLink = $(this);
							var otherData = otherLink.data($.buttoner.DATA);
							otherData.clicked = false;
						});
					}

					$.buttoner._handler(link, settings, event);
				});
			}
		});
	};
	
	$.buttoner = {
		DATA: 'buttoner-data',
		TEXT: 'text',
		IMAGE: 'image',
			
		_handlerKey: function(link) {
			return link.attr('href').substr(0, 1) != '#' ? link.attr('href') : link.attr('href').substr(1);
		},
		_handler: function(link, settings, event) {
			if (settings.handlers != null) {
				var handlerKey = $.buttoner._handlerKey(link);
				if (settings.handlers[handlerKey] != null) {
					settings.handlers[handlerKey](link);
					event.preventDefault();
					return;
				}
			}
			if (settings.handler != null) {
				settings.handler(link);
				event.preventDefault();
			}
		}
	};
	
})(jQuery);